home *** CD-ROM | disk | FTP | other *** search
/ PC-Blue - MS DOS Public Domain Library / PC-Blue MS-DOS Public Domain Library - NYACC.iso / vol320 / hpmac.arc / MAC.C < prev    next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  6.1 KB  |  283 lines

  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <io.h>
  6. #include <dos.h>
  7. #include <math.h>
  8. #include <h/argparse.h>
  9.  
  10. static    char * tutorial[] = {
  11.     "\nFormat is:\n\tmac picname <outname> <-m> <-n> <-t> <-c zz> <-p qqq>",
  12.     "<-x x.x> <-y y.y>\n\n",
  13.     "\tpicname is the name of the input readmac file\n",
  14.     "\toutname (optional) is the output file name\n",
  15.     "\t\twith MAC.LST as a default (device name\n",
  16.     "\t\tsuch as LPT2 is ok also)\n",
  17.     "\t-m\tdon\'t eject after printing\n",
  18.     "\t-n\tproduce negative copy of image\n",
  19.     "\t-t\tadd title (if any) after picture\n",
  20.     "\t-c zz\t(optional) produce zz copies, 0 < zz < 100\n",
  21.     "\t\tdefault number of copies is 1\n",
  22.     "\t-p qqq\t(optional) pells/inch, qqq = 100, 150 or 300\n",
  23.     "\t\tdefault pells is 100\n",
  24.     "\t-x x.x\t(optional) x offset in fractional inches\n",
  25.     "\t\tdefault offset is 1.0 inch from left edge\n",
  26.     "\t-y y.y\t(optional) y offset in fractional inches\n",
  27.     "\t\tdefault offset is 0.5 inch from top edge\n",
  28.     NULL };
  29.     
  30. static    unsigned char buff[74];
  31. static    unsigned char wline[] = "x*b74Wx";
  32. static    unsigned int  bcnt;
  33. FILE    *f = NULL;
  34. int    c = 0;
  35. int    copies = 1;
  36. int    pells = 100;
  37. int    print = 0;
  38. int    neg = 0;
  39. int    x_offset = 810;
  40. int    y_offset = 360;
  41.  
  42. void putline()
  43. {
  44. register int i;
  45.     buff[72] = 0xff;
  46.     write(print,wline,7);
  47.     if(neg != 0)
  48.        for(i=0;i<72;i++)
  49.           buff[i] = ~buff[i];
  50.     write(print,buff,73);
  51. }
  52.  
  53. int expand(rep)
  54. int rep;
  55. {
  56.     rep = 257 - rep;    /* rep count */
  57.     c = fgetc(f);        /* read count */
  58.     if(c == EOF) {
  59.        printf("Error - unexpected eof\n");
  60.        exit(1);
  61.        }
  62.     for(;rep;rep--,bcnt++) {
  63.        buff[bcnt] = c;
  64.        if(bcnt == 72) break;
  65.        }
  66. }
  67.  
  68. int getline()
  69. {
  70. int  i,j;
  71.     bcnt = 0;
  72.     while(bcnt < 72) {
  73.        j = fgetc(f);        /* read count */
  74.        if(j == EOF) {
  75.           printf("Warning - unexpected EOF\n");
  76.           return(1);
  77.           }
  78.        if(j > 127) expand(j);
  79.        else {
  80.               j++;
  81.               if((j+bcnt) > 72) j = 71 - bcnt;
  82.               i = fread(&buff[bcnt],1,j,f);
  83.               if(i != j) {
  84.                  printf("Warning - unexpected EOF\n");
  85.                  return(1);
  86.                  }
  87.               bcnt += j;
  88.           }
  89.        }
  90.        return(0);
  91. }
  92.  
  93. void ioctl(handle)
  94. int  handle;
  95. {
  96. union REGS inregs,outregs;
  97.     inregs.x.ax = 0x4400;
  98.     inregs.x.bx = handle;
  99.     intdos(&inregs,&outregs);
  100.     
  101.     if(outregs.h.dl & 0x80) {
  102.        inregs.x.ax = 0x4401;
  103.        inregs.h.dh = 0;
  104.        inregs.h.dl |= 0x20;
  105.        intdos(&inregs,&outregs);
  106.        }
  107. }
  108.  
  109. char    *set_copy(ch,st)
  110. char    ch;
  111. char    *st;
  112. {
  113.     copies = atoi(st);
  114.     if((copies < 1) || (copies > 99)) {
  115.         copies = 1;
  116.         fprintf(stderr,"-c parm invalid, set to 1\n");
  117.         }
  118.     return(NULL);
  119. }
  120.  
  121. char    *set_pells(ch,st)
  122. char    ch;
  123. char    *st;
  124. {
  125.     pells = atoi(st);
  126.     if((pells != 100) && (pells != 150) && ( pells != 300)) {
  127.         pells = 100;
  128.         fprintf(stderr,"-p parm invalid, set to 100\n");
  129.         }
  130.     return(NULL);
  131. }
  132.  
  133. char    *set_files(ch,st)
  134. char    ch;
  135. char    *st;
  136. {
  137.     if(f == NULL)
  138.         f = fopen(st,"rb");
  139.     else
  140.         print = open(st,O_WRONLY | O_BINARY | O_CREAT | O_TRUNC,S_IWRITE);
  141.     return(NULL);
  142. }
  143.  
  144. char    *set_xy(ch,st)
  145. char    ch;
  146. char    *st;
  147. {
  148. double    d;
  149.     d = atof(st);
  150.     d *= 720.0;
  151.     if((ch == 'x') || (ch == 'X')) x_offset = d;
  152.     if((ch == 'y') || (ch == 'Y')) y_offset = d;
  153.     return(NULL);
  154. }
  155.  
  156. void    setseek()
  157. {
  158. char    tmpbuf[132];
  159. int    i;
  160. long    junk;
  161.  
  162.     fread(tmpbuf,1,132,f);
  163.     tmpbuf[68] = '\0';
  164.     junk = 0l;
  165.     
  166.     if( (strcmp(&tmpbuf[65],"PNT") == 0) ||
  167.       ( (tmpbuf[128] == tmpbuf[129] == tmpbuf[130] == '\0') &&
  168.         ((tmpbuf[131] == '\002') || (tmpbuf[131] == '\003')) ) )
  169.         junk = 640l;
  170.     else
  171.       if( (tmpbuf[0] == tmpbuf[1] == tmpbuf[2] == '\0') &&
  172.         ( (tmpbuf[3] == '\002') || (tmpbuf[3] == '\003')) )
  173.         junk = 512l;
  174.         
  175.     if(junk != 0l) printf("Bypassing %ld bytes ...\n",junk);
  176.     else {
  177.         fprintf(stderr,"Can't determine file type\n");
  178.         exit(1);
  179.         };
  180.    
  181.     i = fseek(f,junk,0);
  182.     if(i) {
  183.        perror("Pattern bypass seek failed");
  184.        exit(1);
  185.        }
  186.     
  187. }
  188.  
  189. void    main(argCount, argVector)
  190. int    argCount;
  191. char    *argVector[];
  192. {
  193. int    i,count;
  194. long    where;
  195. int    f_flag = 0;
  196. int    m_flag = 0;
  197. int    t_flag = 0;
  198. char    *t_text = (char *) 0;
  199.  
  200. #define ArgCount  argCount
  201. #define ArgVector argVector
  202. #include <h/argbegin.h>
  203.     ArgMin        (1)
  204.     ArgMax        (2)
  205.     ArgDescription(tutorial)
  206.     ArgPosCall(set_files)
  207. #include <h/argloop.h>
  208.     ArgTextCall ('c', set_copy)
  209.     ArgTextCall ('C', set_copy)
  210.     ArgTextCall ('p', set_pells)
  211.     ArgTextCall ('P', set_pells)
  212.     ArgTextCall ('x', set_xy)
  213.     ArgTextCall ('X', set_xy)
  214.     ArgTextCall ('y', set_xy)
  215.     ArgTextCall ('Y', set_xy)
  216.     ArgFlagSet  ('n', neg)
  217.     ArgFlagSet  ('N', neg)
  218.     ArgFlagSet  ('m', m_flag)
  219.     ArgFlagSet  ('M', m_flag)
  220.     ArgFlagSet  ('t', t_flag)
  221.     ArgFlagSet  ('T', t_flag)
  222. #include <h/argend.h>
  223.        
  224.     
  225.     if(print == 0)
  226.        print = open("MAC.LST",O_WRONLY | O_BINARY | O_CREAT | O_TRUNC,S_IWRITE);
  227.     
  228.     if(f == NULL) {
  229.        perror("Can\'t open input file");
  230.        exit(1);
  231.        }
  232.        
  233.     if(print == -1) {
  234.        perror("Can\'t open output file");
  235.        exit(1);
  236.        }
  237.        
  238.     ioctl(print);
  239.     setseek();
  240.        
  241.     sprintf(buff,"%c&a%dH%c&a%dV%c&l%dX%c*t%dR%c*r1A",
  242.         0x1b,x_offset,0x1b,y_offset,0x1b,copies,0x1b,pells,0x1b);
  243.     write(print,buff,strlen(buff));
  244.     wline[0] = 0x1b;
  245.     wline[6] = 0xff;
  246.     for(i=0;i<72;i++)
  247.        buff[i] = 0xff;
  248.     for(i=0;i<8;i++)
  249.        putline();
  250.     
  251.                 
  252.     for(count=0;count < 720;count++) {
  253.        if(getline()) break;
  254.        putline();
  255.        }
  256.        
  257.     where = ftell(f);
  258.     printf("Complete at %ld\n",where);
  259.     for(i=0;i<72;i++)
  260.        buff[i] = 0xff;
  261.     for(i=0;i<8;i++)
  262.        putline();
  263.     sprintf(buff,"%c*rB",0x1b);
  264.     write(print,buff,strlen(buff));
  265.     if(t_flag) {
  266.         fseek(f,1l,0);        /* back to get title length */
  267.         i = 0;            /* zero out work integer */
  268.         fread(&i,1,1,f);    /* read into integer, 1 byte */
  269.         if(i > 72) i = 72;    /* truncate msg at 72 */
  270.         count = (80 - i) >> 1;    /* offset for printing */
  271.         sprintf(buff,"\n%c&a%dC",0x1b,count);
  272.         write(print,buff,strlen(buff));
  273.         fread(buff,1,i,f);
  274.         buff[i] = '\0';
  275.         write(print,buff,strlen(buff));
  276.         }
  277.     sprintf(buff,"%cE",0x1b);
  278.     if(!m_flag) write(print,buff,2);
  279.     close(print);
  280.     fclose(f);
  281.     
  282. }
  283.